import { BinaryDecision } from "framework/FSM/DecisionTree/Decision";
 
import { FactoryTaskHelper } from "GameLoop/WorldState/EntityManager/Entitys/room/factory/FactoryTaskHelper";
 
/**
 * 生产的基本资源已经就绪
*/
export class DecisionIsProduceReady <T extends IStateArg<Room>> extends BinaryDecision<T>
{

    public testValue(arg:T): boolean
    {
        const toptask = arg.entity.getTaskByType("factory")[0]?.data ;
        
        const core = arg.entity.getEntity("CoreEntity");
        // global.Log.Info(`toptask :${JSON.stringify(toptask)} arg.entity.Factory:${arg.entity.Factory}`);
        if(toptask && core.Factory)
        {
            const topproduce = FactoryTaskHelper.Top(toptask.arg);
            if(topproduce)
            {
                // global.Log.Info(`${JSON.stringify(toptask)} ->  ${JSON.stringify(topproduce.target)}`)
                if(COMMODITIES[ topproduce.target].level && COMMODITIES[ topproduce.target].level != core.Factory.level)
                {
                    // 不是我这个等级可以生产的
                    return false;
                }
                const subresource = FactoryTaskHelper.CalAllSubResourceAmount(topproduce.target,topproduce.amount-topproduce.number);
                for(let key in subresource)
                {
                    const storecount = core.Factory.store.getUsedCapacity( key as ResourceConstant);
                    if(storecount<subresource[key as FactoryResourceType])
                    {
                        // 资源不足
                        return false
                    }
                }
                return true
            }
        }
        else
        {
            throw new Error(`${arg.entity.GUID} topreactiontask is null`);
        }
        return false
    }

}

